{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55e169d3",
   "metadata": {},
   "source": [
    "### 1、Slicing\n",
    "\n",
    "slicing切片，按照一定条件从列表或者元组中取出部分元素（比如特定范围、索引、分割值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82615e2e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:56:06.453497Z",
     "start_time": "2022-01-20T13:56:06.450420Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   hello   \n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "s = '   hello   '\n",
    "s = s[:]\n",
    "\n",
    "print(s)\n",
    "#    hello\n",
    "\n",
    "\n",
    "s = '   hello   '\n",
    "s = s[3:8]\n",
    "\n",
    "print(s)\n",
    "# hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35bb0812",
   "metadata": {},
   "source": [
    "### 2、strip()\n",
    "\n",
    "strip()方法用于移除字符串头尾指定的字符（默认为空格或换行符）或字符序列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6d2d8c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:56:25.433888Z",
     "start_time": "2022-01-20T13:56:25.430941Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "###hello###\n"
     ]
    }
   ],
   "source": [
    "s = '   hello   '.strip()\n",
    "\n",
    "print(s)\n",
    "# hello\n",
    "\n",
    "\n",
    "s = '###hello###'.strip()\n",
    "\n",
    "print(s)\n",
    "# ###hello###"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "604dde38",
   "metadata": {},
   "source": [
    "在使用strip()方法时，默认去除空格或换行符，所以#号并没有去除。\n",
    "\n",
    "可以给strip()方法添加指定字符，如下所示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4d3c0e2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:56:52.216187Z",
     "start_time": "2022-01-20T13:56:52.213618Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "s = '###hello###'.strip('#')\n",
    "\n",
    "print(s)\n",
    "# hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5505edaf",
   "metadata": {},
   "source": [
    "此外当指定内容不在头尾处时，并不会被去除。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a660713a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:57:05.205232Z",
     "start_time": "2022-01-20T13:57:05.202012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      " \t hello\n",
      " \t hello\n"
     ]
    }
   ],
   "source": [
    "s = ' \\n \\t hello\\n'.strip('\\n')\n",
    "\n",
    "print(s)\n",
    "#\n",
    "#      hello\n",
    "\n",
    "\n",
    "s = '\\n \\t hello\\n'.strip('\\n')\n",
    "\n",
    "print(s)\n",
    "#      hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f4577f",
   "metadata": {},
   "source": [
    "第一个\\n前有个空格，所以只会去取尾部的换行符。\n",
    "\n",
    "最后strip()方法的参数是剥离其值的所有组合，这个可以看下面这个案例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "629bdad0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:57:18.985215Z",
     "start_time": "2022-01-20T13:57:18.982682Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "baidu\n"
     ]
    }
   ],
   "source": [
    "s = 'www.baidu.com'.strip('cmow.')\n",
    "\n",
    "print(s)\n",
    "# baidu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51f081ec",
   "metadata": {},
   "source": [
    "最外层的首字符和尾字符参数值将从字符串中剥离。字符从前端移除，直到到达一个不包含在字符集中的字符串字符为止。\n",
    "\n",
    "在尾部也会发生类似的动作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18c5dde4",
   "metadata": {},
   "source": [
    "### 3、lstrip()\n",
    "\n",
    "移除字符串左侧指定的字符（默认为空格或换行符）或字符序列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d496cccb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:58:10.720476Z",
     "start_time": "2022-01-20T13:58:10.718094Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello   \n"
     ]
    }
   ],
   "source": [
    "s = '   hello   '.lstrip()\n",
    "\n",
    "print(s)\n",
    "# hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5cc9c31",
   "metadata": {},
   "source": [
    "同样的，可以移除左侧所有包含在字符集中的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4a0132dc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:58:22.712268Z",
     "start_time": "2022-01-20T13:58:22.709711Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ee!\n"
     ]
    }
   ],
   "source": [
    "s = 'Arthur: three!'.lstrip('Arthur: ')\n",
    "\n",
    "print(s)\n",
    "# ee!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f852978",
   "metadata": {},
   "source": [
    "### 4、rstrip()\n",
    "\n",
    "移除字符串右侧指定的字符（默认为空格或换行符）或字符序列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f1ee44cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:58:35.842837Z",
     "start_time": "2022-01-20T13:58:35.839341Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   hello\n"
     ]
    }
   ],
   "source": [
    "s = '   hello   '.rstrip()\n",
    "\n",
    "print(s)\n",
    "#    hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed298182",
   "metadata": {},
   "source": [
    "### 5、removeprefix()\n",
    "\n",
    "Python3.9中移除前缀的函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2bc303f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:59:27.164943Z",
     "start_time": "2022-01-20T13:59:27.163466Z"
    }
   },
   "outputs": [],
   "source": [
    "# python 3.9\n",
    "# s = 'Arthur: three!'.removeprefix('Arthur: ')\n",
    "\n",
    "# print(s)\n",
    "# three!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b9e12e",
   "metadata": {},
   "source": [
    "和strip()相比，并不会把字符集中的字符串进行逐个匹配。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87780a63",
   "metadata": {},
   "source": [
    "### 6、removesuffix()\n",
    "\n",
    "Python3.9中移除后缀的函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fc768513",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:00:00.368588Z",
     "start_time": "2022-01-20T14:00:00.366465Z"
    }
   },
   "outputs": [],
   "source": [
    "# s = 'HelloPython'.removesuffix('Python')\n",
    "\n",
    "# print(s)\n",
    "# Hello"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74cc7304",
   "metadata": {},
   "source": [
    "### 7、replace()\n",
    "\n",
    "把字符串中的内容替换成指定的内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0c5e3338",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:00:16.045638Z",
     "start_time": "2022-01-20T14:00:16.043075Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "string-methods-in-python\n",
      "stringmethodsinpython\n"
     ]
    }
   ],
   "source": [
    "s = 'string methods in python'.replace(' ', '-')\n",
    "\n",
    "print(s)\n",
    "# string-methods-in-python\n",
    "\n",
    "\n",
    "s = 'string methods in python'.replace(' ', '')\n",
    "\n",
    "print(s)\n",
    "# stringmethodsinpython"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c58be4f",
   "metadata": {},
   "source": [
    "### 8、re.sub()\n",
    "\n",
    "re是正则的表达式，sub是substitute表示替换。\n",
    "\n",
    "re.sub则是相对复杂点的替换。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "050126e2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:00:28.442854Z",
     "start_time": "2022-01-20T14:00:28.439544Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "string----methods-in-python\n",
      "string-methods-in-python\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "s = \"string    methods in python\"\n",
    "s2 = s.replace(' ', '-')\n",
    "print(s2)\n",
    "# string----methods-in-python\n",
    "\n",
    "\n",
    "s = \"string    methods in python\"\n",
    "s2 = re.sub(\"\\s+\", \"-\", s)\n",
    "print(s2)\n",
    "# string-methods-in-python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d943cb36",
   "metadata": {},
   "source": [
    "和replace()做对比，使用re.sub()进行替换操作，确实更高级点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "889884b5",
   "metadata": {},
   "source": [
    "### 9、split()\n",
    "\n",
    "对字符串做分隔处理，最终的结果是一个列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "12498a94",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:00:53.669322Z",
     "start_time": "2022-01-20T14:00:53.666372Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['string', 'methods', 'in', 'python']\n"
     ]
    }
   ],
   "source": [
    "s = 'string methods in python'.split()\n",
    "\n",
    "print(s)\n",
    "# ['string', 'methods', 'in', 'python']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90c3bc55",
   "metadata": {},
   "source": [
    "当不指定分隔符时，默认按空格分隔。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "21fd9b2d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:01:04.258831Z",
     "start_time": "2022-01-20T14:01:04.256541Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['string methods in python']\n"
     ]
    }
   ],
   "source": [
    "s = 'string methods in python'.split(',')\n",
    "\n",
    "print(s)\n",
    "# ['string methods in python']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735317d2",
   "metadata": {},
   "source": [
    "此外，还可以指定字符串的分隔次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "56a20e4e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:01:14.427090Z",
     "start_time": "2022-01-20T14:01:14.424558Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['string', 'methods in python']\n"
     ]
    }
   ],
   "source": [
    "s = 'string methods in python'.split(' ', maxsplit=1)\n",
    "\n",
    "print(s)\n",
    "# ['string', 'methods in python']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff4418b4",
   "metadata": {},
   "source": [
    "### 10、rsplit()\n",
    "\n",
    "从右侧开始对字符串进行分隔。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "866ca2d3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:01:28.416655Z",
     "start_time": "2022-01-20T14:01:28.414383Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['string methods in', 'python']\n"
     ]
    }
   ],
   "source": [
    "s = 'string methods in python'.rsplit(' ', maxsplit=1)\n",
    "\n",
    "print(s)\n",
    "# ['string methods in', 'python']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "700b23d8",
   "metadata": {},
   "source": [
    "### 11、join()\n",
    "\n",
    "string.join(seq)。以string作为分隔符，将seq中所有的元素(的字符串表示)合并为一个新的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "015a4c5e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:01:41.886030Z",
     "start_time": "2022-01-20T14:01:41.882600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "string-methods-in-python\n",
      "string methods in python\n"
     ]
    }
   ],
   "source": [
    "list_of_strings = ['string', 'methods', 'in', 'python']\n",
    "s = '-'.join(list_of_strings)\n",
    "\n",
    "print(s)\n",
    "# string-methods-in-python\n",
    "\n",
    "\n",
    "list_of_strings = ['string', 'methods', 'in', 'python']\n",
    "s = ' '.join(list_of_strings)\n",
    "\n",
    "print(s)\n",
    "# string methods in python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e49c34c4",
   "metadata": {},
   "source": [
    "### 12、upper()\n",
    "\n",
    "将字符串中的字母，全部转换为大写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "397333ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:02:02.766246Z",
     "start_time": "2022-01-20T14:02:02.764003Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SIMPLE IS BETTER THAN COMPLEX\n"
     ]
    }
   ],
   "source": [
    "s = 'simple is better than complex'.upper()\n",
    "\n",
    "print(s)\n",
    "# SIMPLE IS BETTER THAN COMPLEX"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "134a4177",
   "metadata": {},
   "source": [
    "### 13、lower()\n",
    "\n",
    "将字符串中的字母，全部转换为小写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "12737b37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:02:17.166752Z",
     "start_time": "2022-01-20T14:02:17.164207Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "simple is better than complex\n"
     ]
    }
   ],
   "source": [
    "s = 'SIMPLE IS BETTER THAN COMPLEX'.lower()\n",
    "\n",
    "print(s)\n",
    "# simple is better than complex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d04d215",
   "metadata": {},
   "source": [
    "### 14、capitalize()\n",
    "\n",
    "将字符串中的首个字母转换为大写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5930dccf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:02:28.422128Z",
     "start_time": "2022-01-20T14:02:28.419610Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Simple is better than complex\n"
     ]
    }
   ],
   "source": [
    "s = 'simple is better than complex'.capitalize()\n",
    "\n",
    "print(s)\n",
    "# Simple is better than complex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d86c9b3",
   "metadata": {},
   "source": [
    "### 15、islower()\n",
    "\n",
    "判断字符串中的所有字母是否都为小写，是则返回True，否则返回False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "982b1c61",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:02:40.092717Z",
     "start_time": "2022-01-20T14:02:40.090473Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print('SIMPLE IS BETTER THAN COMPLEX'.islower()) # False\n",
    "\n",
    "print('simple is better than complex'.islower()) # True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab93a6e9",
   "metadata": {},
   "source": [
    "### 16、isupper()\n",
    "\n",
    "判断字符串中的所有字母是否都为大写，是则返回True，否则返回False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9407c6af",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:02:52.899135Z",
     "start_time": "2022-01-20T14:02:52.896361Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('SIMPLE IS BETTER THAN COMPLEX'.isupper()) # True\n",
    "\n",
    "print('SIMPLE IS BETTER THAN complex'.isupper()) # False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92623418",
   "metadata": {},
   "source": [
    "### 17、isalpha()\n",
    "\n",
    "如果字符串至少有一个字符并且所有字符都是字母，则返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e2a372fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:03:07.677163Z",
     "start_time": "2022-01-20T14:03:07.672970Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s = 'python'\n",
    "print(s.isalpha())\n",
    "# True\n",
    "\n",
    "s = '123'\n",
    "print(s.isalpha())\n",
    "# False\n",
    "\n",
    "s = 'python123'\n",
    "print(s.isalpha())\n",
    "# False\n",
    "\n",
    "s = 'python-123'\n",
    "print(s.isalpha())\n",
    "# False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be0c156b",
   "metadata": {},
   "source": [
    "### 18、isnumeric()\n",
    "\n",
    "如果字符串中只包含数字字符，则返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9b1f9c9e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:03:19.595294Z",
     "start_time": "2022-01-20T14:03:19.591502Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s = 'python'\n",
    "print(s.isnumeric())\n",
    "# False\n",
    "\n",
    "s = '123'\n",
    "print(s.isnumeric())\n",
    "# True\n",
    "\n",
    "s = 'python123'\n",
    "print(s.isnumeric())\n",
    "# False\n",
    "\n",
    "s = 'python-123'\n",
    "print(s.isnumeric())\n",
    "# False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9ced10c",
   "metadata": {},
   "source": [
    "### 19、isalnum()\n",
    "\n",
    "如果字符串中至少有一个字符并且所有字符都是字母或数字，则返回True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "320f5f24",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:03:32.282842Z",
     "start_time": "2022-01-20T14:03:32.279183Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s = 'python'\n",
    "print(s.isalnum())\n",
    "# True\n",
    "\n",
    "s = '123'\n",
    "print(s.isalnum())\n",
    "# True\n",
    "\n",
    "s = 'python123'\n",
    "print(s.isalnum())\n",
    "# True\n",
    "\n",
    "s = 'python-123'\n",
    "print(s.isalnum())\n",
    "# False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e1ad1a3",
   "metadata": {},
   "source": [
    "### 20、count()\n",
    "\n",
    "返回指定内容在字符串中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2f3c9bb4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:03:42.746063Z",
     "start_time": "2022-01-20T14:03:42.743434Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "n = 'hello world'.count('o')\n",
    "print(n)\n",
    "# 2\n",
    "\n",
    "n = 'hello world'.count('oo')\n",
    "print(n)\n",
    "# 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea7ee17b",
   "metadata": {},
   "source": [
    "### 21、find()\n",
    "\n",
    "检测指定内容是否包含在字符串中，如果是返回开始的索引值，否则返回-1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2c31c9e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:03:54.422584Z",
     "start_time": "2022-01-20T14:03:54.416920Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "achine Learning\n",
      "-1\n",
      "g\n"
     ]
    }
   ],
   "source": [
    "s = 'Machine Learning'\n",
    "idx = s.find('a')\n",
    "\n",
    "print(idx)\n",
    "print(s[idx:])\n",
    "# 1\n",
    "# achine Learning\n",
    "\n",
    "s = 'Machine Learning'\n",
    "idx = s.find('aa')\n",
    "\n",
    "print(idx)\n",
    "print(s[idx:])\n",
    "# -1\n",
    "# g"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ec4a2e2",
   "metadata": {},
   "source": [
    "此外，还可以指定开始的范围。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "249df7b4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:04:09.314086Z",
     "start_time": "2022-01-20T14:04:09.311103Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "arning\n"
     ]
    }
   ],
   "source": [
    "s = 'Machine Learning'\n",
    "idx = s.find('a', 2)\n",
    "\n",
    "print(idx)\n",
    "print(s[idx:])\n",
    "# 10\n",
    "# arning"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d58bc34e",
   "metadata": {},
   "source": [
    "### 22、rfind()\n",
    "\n",
    "类似于find()函数，返回字符串最后一次出现的位置，如果没有匹配项则返回 -1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "95ca9629",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:04:22.043611Z",
     "start_time": "2022-01-20T14:04:22.040952Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "arning\n"
     ]
    }
   ],
   "source": [
    "s = 'Machine Learning'\n",
    "idx = s.rfind('a')\n",
    "\n",
    "print(idx)\n",
    "print(s[idx:])\n",
    "# 10\n",
    "# arning\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df4cc69d",
   "metadata": {},
   "source": [
    "### 23、startswith()\n",
    "\n",
    "检查字符串是否是以指定内容开头，是则返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d3ad5be1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:04:33.275373Z",
     "start_time": "2022-01-20T14:04:33.272363Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print('Patrick'.startswith('P'))\n",
    "\n",
    "# True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e96faf91",
   "metadata": {},
   "source": [
    "### 24、endswith()\n",
    "\n",
    "检查字符串是否是以指定内容结束，是则返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7b884a42",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:04:48.925297Z",
     "start_time": "2022-01-20T14:04:48.921551Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print('Patrick'.endswith('ck'))\n",
    "\n",
    "# True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66291e02",
   "metadata": {},
   "source": [
    "### 25、partition()\n",
    "\n",
    "string.partition(str)，有点像find()和split()的结合体。\n",
    "\n",
    "从str出现的第一个位置起,把字符串string分成一个3 元素的元组(string_pre_str,str,string_post_str)，如果string中不包含str则 string_pre_str==string。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8fbbaf72",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:05:01.680128Z",
     "start_time": "2022-01-20T14:05:01.675788Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Python ', 'is', ' awesome!')\n",
      "('Python is awesome!', '', '')\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is awesome!'\n",
    "parts = s.partition('is')\n",
    "print(parts)\n",
    "# ('Python ', 'is', ' awesome!')\n",
    "\n",
    "s = 'Python is awesome!'\n",
    "parts = s.partition('was')\n",
    "print(parts)\n",
    "# ('Python is awesome!', '', '')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e878f258",
   "metadata": {},
   "source": [
    "### 26、center()\n",
    "\n",
    "返回一个原字符串居中，并使用空格填充至长度width的新字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3576d873",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:05:15.033643Z",
     "start_time": "2022-01-20T14:05:15.030545Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------Python is awesome!------\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is awesome!'\n",
    "s = s.center(30, '-')\n",
    "\n",
    "print(s)\n",
    "# ------Python is awesome!------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26be55b1",
   "metadata": {},
   "source": [
    "### 27、ljust()\n",
    "\n",
    "返回一个原字符串左对齐，并使用空格填充至长度width的新字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6e0119b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:05:29.403932Z",
     "start_time": "2022-01-20T14:05:29.401279Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is awesome!------------\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is awesome!'\n",
    "s = s.ljust(30, '-')\n",
    "\n",
    "print(s)\n",
    "# Python is awesome!------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e447a7d0",
   "metadata": {},
   "source": [
    "### 28、rjust()\n",
    "\n",
    "返回一个原字符串右对齐，并使用空格填充至长度width的新字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "074087fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:05:42.229177Z",
     "start_time": "2022-01-20T14:05:42.224472Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------Python is awesome!\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is awesome!'\n",
    "s = s.rjust(30, '-')\n",
    "\n",
    "print(s)\n",
    "# ------------Python is awesome!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3186dab",
   "metadata": {},
   "source": [
    "### 29、f-Strings\n",
    "\n",
    "f-string是格式化字符串的新语法。\n",
    "\n",
    "与其他格式化方式相比，它们不仅更易读，更简洁，不易出错，而且速度更快！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d330a6d3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:06:03.148573Z",
     "start_time": "2022-01-20T14:06:03.144909Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is the number 1 in programming!\n",
      "Python is the number 8 in programming!\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "language = 'Python'\n",
    "s = f'{language} is the number {num} in programming!'\n",
    "\n",
    "print(s)\n",
    "# Python is the number 1 in programming!\n",
    "\n",
    "\n",
    "num = 1\n",
    "language = 'Python'\n",
    "s = f'{language} is the number {num*8} in programming!'\n",
    "\n",
    "print(s)\n",
    "# Python is the number 8 in programming!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d71ba67",
   "metadata": {},
   "source": [
    "### 30、swapcase()\n",
    "\n",
    "翻转字符串中的字母大小写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7ee8a77d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:06:14.541261Z",
     "start_time": "2022-01-20T14:06:14.538555Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello WORLD\n"
     ]
    }
   ],
   "source": [
    "s = 'HELLO world'\n",
    "s = s.swapcase()\n",
    "\n",
    "print(s)\n",
    "# hello WORLD"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "038051b5",
   "metadata": {},
   "source": [
    "### 31、zfill()\n",
    "\n",
    "string.zfill(width)。\n",
    "\n",
    "返回长度为width的字符串，原字符串string右对齐，前面填充0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4f2b5616",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T14:06:28.414985Z",
     "start_time": "2022-01-20T14:06:28.411133Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00042\n",
      "-0042\n",
      "+0042\n"
     ]
    }
   ],
   "source": [
    "s = '42'.zfill(5)\n",
    "print(s)\n",
    "# 00042\n",
    "\n",
    "s = '-42'.zfill(5)\n",
    "print(s)\n",
    "# -0042\n",
    "\n",
    "s = '+42'.zfill(5)\n",
    "print(s)\n",
    "# +0042\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b2699b0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
